﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using DunLibrary.Client;
using DunLibrary.CreditCard;
using DunSilverlight.Comm;
using System.Text;

namespace DunSilverlight
{
    public static class AppGlobal
    {

        /// <summary>
        /// 清除缓存数据
        /// </summary>
        public static void Clear()
        {
            banks = null;
            organizations = null;
            controlTypes = null;
            contactTypes = null;
            receiveTypeCalls = null;
            invTypes = null;

            CurrentUser = null;
            CurrentRights = null;
        }

        private static IList<DunLibrary.Client.Company> banks;//银行集合的缓存

        private static IList<DunLibrary.Organization.Organization> organizations;//组织机构缓存

        private static IList<DunLibrary.Dun.ControlType> controlTypes;//管控类型集合缓存

        private static IList<DunLibrary.Base.ContactType> contactTypes;//联系方式类型集合缓存

        private static IList<DunLibrary.Dun.ReceiveTypeOfCall> receiveTypeCalls;//联系结果集合缓存

        private static IList<DunLibrary.Base.InvestigationType> invTypes;//第三方调查类型

        public static IList<int> Periods = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };//期间集合1-12月
        /// <summary>
        /// 当前用户
        /// </summary>
        private static DunLibrary.User.User currentUser;
        public static DunLibrary.User.User CurrentUser
        {
            get
            {
                return currentUser;
            }
            set
            {
                currentUser = value;
                if (value != null)
                {
                    currentUser.Password = Convert.ToBase64String(Encoding.UTF8.GetBytes(currentUser.Password));
                }
            }
        }

        /// <summary>
        /// 当前用户权限
        /// </summary>
        public static IList<DunLibrary.User.Right> CurrentRights { get; set; }

        /// <summary>
        /// 银行列表的缓存，避免频繁访问数据库
        /// </summary>
        public static void  GetBanks(Action<IList<Company>> callBack)
        {
            if (banks == null)
            {
                BankService.BankServiceClient ser = new BankService.BankServiceClient();

                ser.GetListByHQLCompleted += (object sender1, BankService.GetListByHQLCompletedEventArgs e1) =>
                    {
                        int totalCount = 0;
                        banks = DunSilverlight.Comm.JsonSerializerHelper.JsonToEntities<Company>(e1.Result, out totalCount);
                        callBack(banks);
                    };
                ser.GetListByHQLAsync("from Company");
            }
            else
            {
                callBack(banks);
            }

        }

        /// <summary>
        /// 组织机构缓存，避免频繁访问数据库
        /// </summary>
        public static void GetOrganizations(Action<IList<DunLibrary.Organization.Organization>> callBack)
        {
            if (organizations == null)
            {
                OrganziationService.OrganizationServiceClient ser = new OrganziationService.OrganizationServiceClient();

                ser.GetListByHQLCompleted += (object sender1, OrganziationService.GetListByHQLCompletedEventArgs e1) =>
                {
                    int totalCount = 0;
                    organizations = DunSilverlight.Comm.JsonSerializerHelper.JsonToEntities<DunLibrary.Organization.Organization>(e1.Result, out totalCount);
                    callBack(organizations);//回调
                };
                ser.GetListByHQLAsync("from Organization");
            }
            else
            {
                callBack(organizations);
            }
        }
        /// <summary>
        /// 获取管控类型
        /// </summary>
        /// <param name="callBack"></param>
        public static void GetControlTypes(Action<IList<DunLibrary.Dun.ControlType>> callBack)
        {
            if (controlTypes == null)
            {
                ControlTypeService.ControlTypeServiceClient ser = new ControlTypeService.ControlTypeServiceClient();

                ser.GetListByHQLCompleted += (object sender1, ControlTypeService.GetListByHQLCompletedEventArgs e1) =>
                {
                    try
                    {
                        int totalCount = 0;
                        controlTypes = DunSilverlight.Comm.JsonSerializerHelper.JsonToEntities<DunLibrary.Dun.ControlType>(e1.Result, out totalCount);
                        callBack(controlTypes);//回调
                    }
                    catch (Exception ex)
                    {
                        Tools.ShowMessage(ex.Message, "", false);
                    }
                };
                ser.GetListByHQLAsync("from ControlType");
            }
            else
            {
                callBack(controlTypes);
            }
        }

        /// <summary>
        /// 获取联系方式类型集合
        /// </summary>
        /// <param name="callBack"></param>
        public static void GetContactTypes(Action<IList<DunLibrary.Base.ContactType>> callBack)
        {

            


            if (contactTypes == null)
            {
                ContactTypeService.ContactTypeServiceClient ser = new ContactTypeService.ContactTypeServiceClient();

                ser.GetListByHQLCompleted += (object sender1, ContactTypeService.GetListByHQLCompletedEventArgs e1) =>
                {
                    int totalCount = 0;
                    contactTypes = DunSilverlight.Comm.JsonSerializerHelper.JsonToEntities<DunLibrary.Base.ContactType>(e1.Result, out totalCount);
                    callBack(contactTypes);//回调
                };
                ser.GetListByHQLAsync("from ContactType");
            }
            else
            {
                callBack(contactTypes);
            }
        }

        /// <summary>
        /// 获取联系结果集合
        /// </summary>
        /// <param name="callBack"></param>
        public static void GetReceiveTypeCalls(Action<IList<DunLibrary.Dun.ReceiveTypeOfCall>> callBack)
        {
            if (receiveTypeCalls == null)
            {
                ReceiveTypeOfCallService.ReceiveTypeOfCallServiceClient ser = new ReceiveTypeOfCallService.ReceiveTypeOfCallServiceClient();

                ser.GetListByHQLCompleted += (object sender1, ReceiveTypeOfCallService.GetListByHQLCompletedEventArgs e1) =>
                {
                    int totalCount = 0;
                    receiveTypeCalls = DunSilverlight.Comm.JsonSerializerHelper.JsonToEntities<DunLibrary.Dun.ReceiveTypeOfCall>(e1.Result, out totalCount);
                    callBack(receiveTypeCalls);//回调
                };
                ser.GetListByHQLAsync("from ReceiveTypeOfCall");
            }
            else
            {
                callBack(receiveTypeCalls);
            }
        }
        /// <summary>
        /// 获得第三方查询类型
        /// </summary>
        /// <param name="invTypes"></param>
        public static void GetInvTypes(Action<IList<DunLibrary.Base.InvestigationType>> callBack)
        {
            if (invTypes == null)
            {
                InvTypeService.InvestigationTypeServiceClient ser = new InvTypeService.InvestigationTypeServiceClient();
                ser.GetListByHQLCompleted += (object sender, InvTypeService.GetListByHQLCompletedEventArgs e) =>
                    {
                        int totalCount = 0;
                        invTypes = JsonSerializerHelper.JsonToEntities<DunLibrary.Base.InvestigationType>(e.Result, out totalCount);

                        callBack(invTypes);
                    };
                ser.GetListByHQLAsync("from InvestigationType");
            }
            else
            {
                callBack(invTypes);
            }
        }

        /// <summary>
        /// 检查当前用户是否有某个权限
        /// </summary>
        /// <param name="rightNo"></param>
        /// <returns></returns>
        public static bool CheckRight(string rightNo)
        {
            foreach (DunLibrary.User.Right r in CurrentRights)
            {
                if (r.Number == rightNo)
                {
                    return true;
                }
            }
            return false;
        }
        /// <summary>
        /// 设置通讯方式是否有效
        /// </summary>
        /// <param name="contact"></param>
        /// <param name="callBack"></param>
        public static void SetContactValid(CardholderContact contact, Action callBack)
        {
            CardholderContactService.CardholderContactServiceClient ser = new CardholderContactService.CardholderContactServiceClient();

            ser.SaveOrUpdateCompleted += (object sender, CardholderContactService.SaveOrUpdateCompletedEventArgs e) =>
                {
                    //保存成功！
                    if (JsonSerializerHelper.JsonToEntity<int>(e.Result)>0)
                    {
                        callBack();
                    }
                };
            string json=Newtonsoft.Json.JsonConvert.SerializeObject(contact);
            ser.SaveOrUpdateAsync(json);
        }
    }
}
